﻿using Microsoft.Xna.Framework.Content;
using Imaginecup.util.resourceMan;
using Microsoft.Xna.Framework.Graphics;
using Imaginecup.util;
using Imaginecup.factory;
using Microsoft.Xna.Framework;
using imaginecup.util;
using Imaginecup.sprite;
using Imaginecup.util.collision;
using Imaginecup.gameState.choiceMap;
using System.Collections.Generic;

namespace Imaginecup.gameState.temp
{
    public class ChoiceMap : GameState
    {
        private ContentManager m_Content;

        private static ChoiceMap choiceMap = null;

        private IconMan m_WaterIcon;
        private IconMan m_MalariaIcon;
        private IconMan m_RecycleIcon;
     //   private IconMan m_DarkIcon;

        private EntityInfo m_ExitInfo;
        private ISprite m_ExitSprite;

        private bool m_bAllClear;

        private Dictionary<StateStageManID, IconMan> m_IconManDic;
        private Vector2 iconSourceSize = new Vector2(50, 50);

        private ChoiceMap()
        {
            m_Content = new ContentManager(m_Game.Services);
            m_Content.RootDirectory = "Content";

            m_WaterIcon = new IconMan(StateStageManID.WaterStageMan_1, iconSourceSize);
            m_WaterIcon.SetStateFactoryID(StateFactoryID.State_WaterGame);

            m_RecycleIcon = new IconMan(StateStageManID.RecycleStageMan_1, iconSourceSize);

            m_MalariaIcon = new IconMan(StateStageManID.MalariaStageMan_1, iconSourceSize);
            m_MalariaIcon.SetStateFactoryID(StateFactoryID.State_Malaria);
           // m_DarkIcon = new IconMan(StateStageManID.Dark_1, iconSourceSize);

            m_IconManDic = new Dictionary<StateStageManID, IconMan>();

            m_IconManDic.Add(StateStageManID.WaterStageMan_1, m_WaterIcon);
            m_IconManDic.Add(StateStageManID.RecycleStageMan_1, m_RecycleIcon);
            m_IconManDic.Add(StateStageManID.MalariaStageMan_1, m_MalariaIcon);
        //    m_IconManDic.Add(StateStageManID.Dark_1, m_DarkIcon);
        }

        public static ChoiceMap GetChoiceMap()
        {
            if (choiceMap == null)
            {
                choiceMap = new ChoiceMap();
            }
            return choiceMap;
        }

        /************************************************************************/
        /* 초기화 되는 값                                                       */
        /************************************************************************/
        protected override void InitState()
        {
            m_WaterIcon.SetEntityInfo( new EntityInfo(300 , 350 , 62 , 64) );
            m_MalariaIcon.SetEntityInfo(new EntityInfo(230, 100, 82, 66));
            m_RecycleIcon.SetEntityInfo(new EntityInfo(450, 250, 62, 59));
         //   m_DarkIcon.SetEntityInfo(new EntityInfo(700, 100, 50, 50));


            m_ExitInfo = new EntityInfo(724, 24, 48, 50);
            m_bAllClear = false;
        }


        /************************************************************************/
        /* ContentMan에 리소스 로드(배경,메뉴 리소스)                                  */
        /************************************************************************/
        protected override void LoadStateResource()
        {
            SoundMan.GetSoundMan().LoadSong("ChoiceBGM", "sound\\Choice");
            SoundMan.GetSoundMan().PlayBackSound("ChoiceBGM");


            ResourceMan resourceMan = ResourceMan.GetResourceMan();
            resourceMan.SetTextureFrame("Back", m_Content.Load<Texture2D>("choiceMap\\Map"));
            resourceMan.SetTextureFrame("Malaria_Icon", m_Content.Load<Texture2D>("choiceMap\\Malaria_Icon"));
            resourceMan.SetTextureFrame("Water_Icon", m_Content.Load<Texture2D>("choiceMap\\water_Icon"));
            resourceMan.SetTextureFrame("Recycle_Icon", m_Content.Load<Texture2D>("choiceMap\\recycle_Icon"));
          //  resourceMan.SetTextureFrame("DarkRain_Icon", m_Content.Load<Texture2D>("choiceMap\\dark_Rain"));
            resourceMan.SetTextureFrame("Exit", m_Content.Load<Texture2D>("ui\\sel_exit"));


            ChangeSourceSizeSprite sprite = new ChangeSourceSizeSprite();
            sprite.SetTextureName("Malaria_Icon");
            m_MalariaIcon.SetSprite(sprite);
            sprite.SetSourceInfoClass(m_MalariaIcon);

            sprite = new ChangeSourceSizeSprite();
            sprite.SetTextureName("Water_Icon");
            sprite.SetSourceInfoClass(m_WaterIcon);

            m_WaterIcon.SetSprite(sprite);

            sprite = new ChangeSourceSizeSprite();
            sprite.SetTextureName("Recycle_Icon");
            sprite.SetSourceInfoClass(m_RecycleIcon);

            m_RecycleIcon.SetSprite(sprite);


        //    sprite = new ChangeSourceSizeSprite();
       //     sprite.SetTextureName("DarkRain_Icon");
         //   sprite.SetSourceInfoClass(m_DarkIcon);

       //     m_DarkIcon.SetSprite( sprite );

            NormalSprite normalSprite = new NormalSprite();
            normalSprite.SetTextureName("Exit");

            m_ExitSprite = normalSprite;
        }


        /************************************************************************/
        /* 리소스해제                                                                     */
        /************************************************************************/
        protected override void UnloadResource()
        {
            m_WaterIcon.Release();
            m_RecycleIcon.Release();
            m_MalariaIcon.Release();
          //  m_DarkIcon.Release();

            ResourceMan.GetResourceMan().Release();

            SoundMan.GetSoundMan().ReleaseSound();
            m_Content.Unload();

        }



        public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (m_bAllClear)
            {
                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_Ending));
                return;
            }

            IMouseInputHandler mouseInput = MouseInputHandler.GetInputHandler();

            foreach(KeyValuePair<StateStageManID , IconMan> icon in m_IconManDic)
            {
                icon.Value.Update();
            }
            //for( )

            if (m_MalariaIcon.IsSelect())
            {
                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_Malaria));
            }

            else if (m_WaterIcon.IsSelect())
            {
                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_WaterGame));
            }

            else if (m_RecycleIcon.IsSelect())
            {
                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_RecycleGame));
            }

            //else if (m_DarkIcon.IsSelect())
            //{
            //    ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_RainGame));
            //}

            else if (mouseInput.IsOneClickUp() && Collision.CollisionByPoint(m_ExitInfo, mouseInput.Position)) 
            {
                ChangeState((IGameState)StateFactory.GetStateFactory().GetObject(StateFactoryID.State_StartBtn));
            }

        }
        
        public override void Draw(GameTime gameTime)
        {
            if (m_bAllClear) return;

            SpriteBatch sb = m_GraphicData.GetSpriteBatch();

            sb.Begin();
            sb.Draw(ResourceMan.GetResourceMan().GetTexture("Back"), Vector2.Zero, Color.White);
            sb.End();

            m_MalariaIcon.Draw(gameTime, sb);
            m_WaterIcon.Draw(gameTime, sb);
            m_RecycleIcon.Draw(gameTime, sb);
          //  m_DarkIcon.Draw(gameTime, sb);
            m_ExitSprite.Draw(sb , m_ExitInfo);

            base.Draw(gameTime);
        }

        public void ClearState(StateStageManID stateManID)
        {
            if( m_IconManDic.ContainsKey( stateManID ) )
            {
                m_IconManDic[stateManID].SetClear();
                m_bAllClear = GetClearState();
            }
        }
        private bool GetClearState()
        {
            foreach (KeyValuePair<StateStageManID, IconMan> icon in m_IconManDic)
            {
                if (icon.Value.GetClear() == false)
                {
                    return false;
                }
            }
            return true;
        }
    }
}
